package com.z.action;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import com.z.base.BaseAction;
import com.z.service.ViewService;
import com.z.util.AttachUtil;
import com.z.util.JsonUtil;
import com.z.vo.ViewForm;

@SuppressWarnings("serial")
public class ViewAction extends BaseAction {
	
	private ViewForm viewForm=new ViewForm();
	
	@Override
	public Object getModel() {
		return viewForm;
	}
	
	public String execute(){
		return SUCCESS;
	}
	
	public String list(){
		ViewService.getInstance().findList(viewForm);
		return renderJson(JsonUtil.getInstance().toJson(viewForm.getPage()));
	}
	
	/**
	 * 上传
	 * @return
	 */
	public String add(){
		return "add";
	}
	
	public String delete(){
		ViewService.getInstance().delete(viewForm.getDocId());
		return null;
	}
	
	public String upload(){
		ViewService.getInstance().saveUploadFile(viewForm,getLoginUser());
		return "addResult";
	}
	
	public String display(){
		return "display";
	}
	
	public String displaySwf(){
		ViewService.getInstance().findDocument(viewForm);
		InputStream in = null;
        try {
            ServletOutputStream out = getResponse().getOutputStream();
            byte[] buffer = AttachUtil.getInstance().read(viewForm.getSwfPath());
            out.write(buffer);
            out.close();
        } catch (Throwable e) {
        } finally {
        	if(in!=null){
	            try {
	                in.close();
	            } catch (IOException e) {
	            }
        	}
        }
		return null;
	}
}
